package com.bangdao.app.jetpacktest.architecture.mvp.view;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bangdao.app.jetpacktest.R;
import com.bangdao.app.jetpacktest.architecture.mvp.model.User;
import com.bangdao.app.jetpacktest.architecture.mvp.presenter.IUserPresenter;
import com.bangdao.app.jetpacktest.architecture.mvp.presenter.UserPresenter;

import java.util.List;

/**
 * MVP实现
 */
public class UserListMVPActivity extends AppCompatActivity implements IUserView{

    private IUserPresenter userPresenter;

    private ProgressBar mProgressBar;
    private RecyclerView mRvUserList;
    private UserAdapter mUserAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_list);

        //创建Presenter
        userPresenter = new UserPresenter(this);

        initView();
        getData();
    }

    private void initView() {
        mProgressBar = findViewById(R.id.pb_loading_users);
        mRvUserList = findViewById(R.id.rv_user_list);

        mRvUserList.setLayoutManager(new LinearLayoutManager(this));
        mUserAdapter = new UserAdapter(R.layout.item_user_mvvm);
        mRvUserList.setAdapter(mUserAdapter);
    }

    /**
     * 获取数据，
     */
    private void getData() {
        userPresenter.getUserInfo();
    }


    @Override
    public void showProgressBar(boolean isVisible) {
        mProgressBar.setVisibility(isVisible? View.VISIBLE:View.GONE);
    }

    @Override
    public void showUserList(List<User> users) {
        if (users == null) {
            Toast.makeText(UserListMVPActivity.this, "获取user失败！", Toast.LENGTH_SHORT).show();
            return;
        }
        mUserAdapter.setNewInstance(users);
    }
}